package com.whut.common;

import com.whut.model.UrlProperties;
import com.whut.utils.FileUtil;
import com.whut.utils.MDoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author: liukang
 * @Description: 报告里面要改地址的一些方法
 * @date: 2020/11/5 10:54
 */
@Component
public class ReportCommonFunc {


    //private final static String reportEnvironment = "dev";      //本地开发
    private final static String reportEnvironment = "prod";     //正式服环境

    //本地开发插入图片的固定地址
    private final static String localNoPicPath = "D:\\Work\\LabWork\\YiRui\\文档\\Test\\Pic\\noPic.png";

    //本地开发报告生成地址
    private final static String localReportPath = "D:\\Work\\LabWork\\YiRui\\文档\\Test\\";
    //private final static String localReportPath = "E:\\tencent_File\\1296928795\\FileRecv\\yirui\\";

    private static UrlProperties urlProperties;
    @Autowired
    public ReportCommonFunc(UrlProperties urlProperties) {
        ReportCommonFunc.urlProperties=urlProperties;
    }

    //根据图片地址生成noPic图片编码
    public static String generateNoPicImageCode(){

        if(reportEnvironment.equals("dev")){
            //本地测试
            return MDoc.getImageStr(localNoPicPath);
        }else{
            //正式服环境
            return MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
        }

    }

    //根据图片地址生成图片编码，本地和测试服都默认图片
    public static String generateImageCode(String ImageStr){

        if(reportEnvironment.equals("dev")){
            //本地测试
            return MDoc.getImageStr(localNoPicPath);
        }else{
            //正式服环境
            return MDoc.getImageStr(ImageStr);
//            return MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
        }

    }

    //根据数据结果和文件名称生成报告，返回报告地址
    public static String generateReport(Map dataMap, String reportName, String ftlName){
        MDoc mdoc = new MDoc();
        String path = FileUtil.getServerFolderPath();
        File f1 = new File(path, "doc");
        if (!f1.exists()) {
            f1.mkdirs();
        }

        String result = "";

        if(reportEnvironment.equals("dev")){

            //报告本地测试使用
            try {
                mdoc.createDoc(dataMap, localReportPath + reportName + ".doc", "ftl\\"+ ftlName +".ftl");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //MDoc.docToDocx(localReportPath + reportName + ".doc", localReportPath + reportName + ".doc");
            result = localReportPath + reportName + ".doc";

        }else {

            //正式服环境
            String tomcatDocPath = "D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\";
            try {
                mdoc.createDoc(dataMap, path + "\\doc\\" + reportName + ".doc", "ftl\\"+ ftlName +".ftl");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //MDoc.docToDocx(tomcatDocPath + reportName + ".doc", tomcatDocPath + reportName + ".doc");
            result= urlProperties.getAddress() +"/doc/"  + reportName + ".doc";

        }
        return result;
    }



    //根据数字序号返回相应的汉字序号，例如 1 =》 （一）
    public static String getChineseNumber(int i){
        String[] chineseNumberArray = {"十","一","二","三","四","五","六","七","八","九"};
        return "（"+ chineseNumberArray[i] + "）";
    }

    public static String num2Chinese(int section) {
        if (section >= 10 && section < 20)
            return "十" + num2Chinese(section % 10);
        String[] chnNumChar = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] chnUnitChar = {"", "十", "百", "千"};
        StringBuilder chnStr = new StringBuilder();
        StringBuilder strIns = new StringBuilder();
        int unitPos = 0;
        boolean zero = true;
        while (section > 0) {
            int v = section % 10;
            if (v == 0) {
                if (!zero) {
                    zero = true;
                    chnStr.append(chnNumChar[v]).append(chnStr);
                }
            } else {
                zero = false;
                strIns.delete(0, strIns.length());
                strIns.append(chnNumChar[v]);
                strIns.append(chnUnitChar[unitPos]);
                chnStr.insert(0, strIns);
            }
            unitPos++;
            section = (int) Math.floor(section / 10f);
        }
        return chnStr.toString();
    }



}
